package com.wc.test.service;

import java.sql.SQLException;
import java.util.List;

import com.wc.test.bean.User;
import com.wc.test.dao.UserDaoImpl;
import com.wc.test.util.JDBCUtils;
import com.wc.test.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User业务逻辑实现类
 * 
 * @author ysh
 */
public class UserServiceImpl implements UserService {

	/**
	 * 单例模式的饿汉式，空间换时间，无论你用不用都已经创建
	 */
	// 私有的构造函数，保证外类不能实例化本类
	private UserServiceImpl() {
		
	}

	// 自己创建一个类的实例化
	private static final UserServiceImpl service = new UserServiceImpl();

	// 创建一个get方法，返回一个实例
	public static UserServiceImpl getInstance() {
		return service;
	}


	public int findCount() {
		int count = 0;
		try {
			count = UserDaoImpl.getInstance().findCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	public boolean saveUser(User user) {
		boolean flag = false;
			try {
				// 开启事务
				JDBCUtils.startTransaction();
				if (StringUtils.isAllEmpty(user.getUsername(),user.getPassword())) {
					throw new Exception("基本信息不能为空");
				}
				//根据用户名查不到用户，即可添加
				if(validateUserName(user.getUsername()) == true) {
					throw new Exception("用户名不能相等");
				}
				int result = UserDaoImpl.getInstance().saveUser(user);
				if (result > 0) {
					flag = true;
					// 提交事务
					JDBCUtils.commitTransaction();
				}
				
			} catch (Exception e) {
				System.out.println(e.getMessage());
				// 回滚事务
				try {
					JDBCUtils.rollbackTransaction();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		return flag;
	}

	public boolean updateUser(User user) {
		boolean flag = false;
			try {
				// 开启事务
				JDBCUtils.startTransaction();
				//保证数据完整性，已存在数据进行修改时应该抛出异常，回滚事物。
				if (StringUtils.isAllEmpty(user.getUsername(),user.getPassword())) {
					throw new Exception("基本信息不能为空");
				}
				
				if (getUserByID(user.getId()) == null) {
					throw new Exception("用户不能为空");
				}
				
				if(validateUserPwd(user.getPassword()) == true) {
					throw new Exception("原密码一致，修改没有意义");
				}
				
				if(validateUserName(user.getUsername())) {
					throw new Exception("原用户名一致，修改没有意义");
				}
				
				int result = UserDaoImpl.getInstance().updateUser(user);
					if (result > 0) {
						flag = true;
						// 提交事务
						JDBCUtils.commitTransaction();
					}
				
			} catch (Exception e) {
				System.out.println(e.getMessage());
				// 回滚事务
				try {
					JDBCUtils.rollbackTransaction();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			
		}
		return flag;
	}


	public boolean removeUser(int id) {
		boolean flag = false;
			try {
				// 开启事务
				JDBCUtils.startTransaction();
				int result = UserDaoImpl.getInstance().removeUser(id);
				if (result > 0) {
					flag = true;
				}
				// 提交事务
				JDBCUtils.commitTransaction();
			} catch (Exception e) {
				System.out.println(e.getMessage());
				// 回滚事务
				try {
					JDBCUtils.rollbackTransaction();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		return flag;
	}
	
	public User getUserByID(int id) {
		User user = null;
			try {
				user = UserDaoImpl.getInstance().getUserByID(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		return user;
	}

	public List<User> getUserList(String userName, String isSort, int currentPageNo, int pageSize) {
		List<User> userList = null;
		try {
			userList = UserDaoImpl.getInstance().getUserList(userName,isSort,currentPageNo,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userList;

	}


	public User login(String loginName, String password) {
		User user = null;
		try {
			user = UserDaoImpl.getInstance().getLoginUser(loginName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//进行密码匹配
        if (user != null) {//如果查到了那么就进行用户密码对比，不相等返回null，相等就依旧返回一个用户对象
            if (!user.getPassword().equals(password)) {
                user = null;
            }
        }
		return user;
	}


	public User registerUser(String userName) {
		
		User user = null;
		try {
			user = UserDaoImpl.getInstance().getLoginUser(userName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}


	public boolean validateUserName(String username) {
		boolean flag = false;
		try {
			flag = UserDaoImpl.getInstance().validateUserName(username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}



	public boolean validateUserPwd(String password) {
		boolean flag = false;
		try {
			flag = UserDaoImpl.getInstance().validateUserPwd(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}





	
	
	

}
